package springboot.springboot06_ssmp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import springboot.common_utils.R;
import springboot.springboot06_ssmp.entity.Book;
import springboot.springboot06_ssmp.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author daiqiang
 * @since 2022/1/12-22:19
 */
@CrossOrigin  //允许跨域
@RestController
@RequestMapping("books")
public class BookController {
    @Autowired

    private IBookService bookService;


    @GetMapping
    public R getAll(){
        return R.success(bookService.list());
    }
    @PostMapping
    public R save(@RequestBody Book book){
        boolean flag=bookService.save(book);
        if(flag) {
            return R.success("success");
        }else{
            return R.fail("fail");
        }
    }
    @PutMapping
    public  R update(@RequestBody Book book){
        return  R.success(bookService.modify(book));
    }
    @DeleteMapping("{id}")
    public  R delete(@PathVariable Integer id){
        System.out.println("11111111111");
        return  R.success(bookService.removeById(id));
    }
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id) {

        return  R.success(bookService.getById(id));
    }
  @GetMapping("{currentPage}/{pageSize}")
  public R getAllPage(@PathVariable int currentPage,@PathVariable int pageSize,Book book){
      System.out.println("参数----》"+book);
      IPage<Book> pageBook=bookService.getPage(currentPage,pageSize,book );
      return R.success(pageBook);

  }
}

